package com.cyt.web.controller.work.enrollment;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.web.controller.base.BaseController;
import com.cyt.work.enrollment.model.po.EnrollmentHomeinfo;
import com.cyt.work.enrollment.model.po.EnrollmentRelease;
import com.cyt.work.enrollment.model.po.EnrollmentStuinfo;
import com.cyt.work.enrollment.model.po.EnrollmentTest;
import com.cyt.work.enrollment.model.vo.EnrollmentHomeinfoVo;
import com.cyt.work.enrollment.model.vo.EnrollmentReleaseVo;
import com.cyt.work.enrollment.model.vo.EnrollmentStuinfoVo;
import com.cyt.work.enrollment.model.vo.EnrollmentTestVo;
import com.cyt.work.enrollment.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 自主招生-招生学生信息控制层
 *
 * @author caideyong
 * @date 2019-11-22 16:30:39
 */
@RestController
public class EnrollmentStuinfoController extends BaseController {

    @Resource
    private EnrollmentStuinfoService enrollmentStuinfoService;
    @Autowired
    private EnrollmentRuleService enrollmentRuleService;
    @Autowired
    private EnrollmentHomeinfoService enrollmentHomeinfoService;
    @Autowired
    private EnrollmentTestService enrollmentTestService;
    @Autowired
    private EnrollmentReleaseService enrollmentReleaseService;
    /**
     * 统一跳转
     *
     * @param pageName 页面名称
     * @param applyId  应用id
     * @return ModelAndView
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @RequestMapping("/enrollment/enrollmentstuinfo/{pageName}")
    public ModelAndView goPage(@PathVariable("pageName") String pageName, String applyId) {
        ModelAndView view = new ModelAndView("/work/enrollment/enrollmentstuinfo/" + pageName);
        view.addObject("applyId", applyId);
        return view;
    }

    /**
     * 分页查询
     *
     * @param enrollmentStuinfoVo 接受参数对象
     * @param page                当前页面
     * @param limit               页面数量
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @BaseLog(module = "自主招生", classz = "招生学生信息", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentstuinfo/page", method = RequestMethod.POST)
    public ReturnResult page(EnrollmentStuinfoVo enrollmentStuinfoVo, int page, int limit) {
        return enrollmentStuinfoService.page(enrollmentStuinfoVo, page, limit);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @BaseLog(module = "自主招生", classz = "招生学生信息", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentstuinfo/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id,String releaseId,String parentMenuId) {
        ModelAndView view = new ModelAndView("/work/enrollment/enrollmentstuinfo/enrollmentStuinfoEdit");
        EnrollmentStuinfo enrollmentStuinfo=enrollmentStuinfoService.selectByPrimaryKey(id);
        view.addObject("releaseId",releaseId);
        view.addObject("parentMenuId",parentMenuId);
        if (StringUtils.isNotBlank(id)) {
            view.addObject("enrollmentStuinfo",enrollmentStuinfoService.selectByPrimaryKey(id));
            view.addObject("enrollmentRelease",enrollmentRuleService.selectByPrimaryKey(releaseId));
            EnrollmentHomeinfoVo enrollmentHomeinfoVo=new EnrollmentHomeinfoVo();
            EnrollmentTestVo enrollmentTestVo=new EnrollmentTestVo();
            enrollmentTestVo.setStuinfoId(enrollmentStuinfo.getId());
            enrollmentHomeinfoVo.setStuinfoId(enrollmentStuinfo.getId());
            List<EnrollmentHomeinfo> enrollmentHomeinfoList=enrollmentHomeinfoService.listExample(enrollmentHomeinfoVo);
            List<EnrollmentTest> enrollmentTestList=enrollmentTestService.listExample(enrollmentTestVo);
            view.addObject("homeinfoList",enrollmentHomeinfoList);
            view.addObject("testList",enrollmentTestList);
        }
        return view;
    }
    /**
     * 跳转详情界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @BaseLog(module = "自主招生", classz = "招生学生信息", methods = "跳转详情界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentstuinfo/getDetail", method = RequestMethod.GET)
    public ModelAndView getDetail(String id) {
        ModelAndView view = new ModelAndView("/work/enrollment/enrollmentstuinfo/enrollmentStuinfoDetail");
        EnrollmentStuinfo enrollmentStuinfo=enrollmentStuinfoService.selectByPrimaryKey(id);
        if (StringUtils.isNotBlank(id)) {
            view.addObject("enrollmentStuinfo",enrollmentStuinfoService.selectByPrimaryKey(id));
            EnrollmentHomeinfoVo enrollmentHomeinfoVo=new EnrollmentHomeinfoVo();
            EnrollmentTestVo enrollmentTestVo=new EnrollmentTestVo();
            enrollmentTestVo.setStuinfoId(enrollmentStuinfo.getId());
            enrollmentHomeinfoVo.setStuinfoId(enrollmentStuinfo.getId());
            List<EnrollmentHomeinfo> enrollmentHomeinfoList=enrollmentHomeinfoService.listExample(enrollmentHomeinfoVo);
            List<EnrollmentTest> enrollmentTestList=enrollmentTestService.listExample(enrollmentTestVo);
            view.addObject("homeinfoList",enrollmentHomeinfoList);
            view.addObject("testList",enrollmentTestList);
        }
        return view;
    }
    /**
     * 跳转审核界面
     *
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @BaseLog(module = "自主招生", classz = "招生学生信息", methods = "跳转一键审核界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentstuinfo/getCheckAll", method = RequestMethod.GET)
    public ModelAndView getCheckAll(String releaseId,String parentMenuId) {
        ModelAndView view = new ModelAndView("/work/enrollment/enrollmentstuinfo/enrollmentStuinfoCheckAll");
        view.addObject("releaseId",releaseId);
        view.addObject("parentMenuId",parentMenuId);
        return view;
    }
    /**
     * 跳转审核界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @BaseLog(module = "自主招生", classz = "招生学生信息", methods = "跳转审核界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentstuinfo/getCheck", method = RequestMethod.GET)
    public ModelAndView getCheck(String id,String parentMenuId,String releaseId) {
        ModelAndView view = new ModelAndView("/work/enrollment/enrollmentstuinfo/enrollmentStuinfoCheck");
        EnrollmentStuinfo enrollmentStuinfo=enrollmentStuinfoService.selectByPrimaryKey(id);
        view.addObject("parentMenuId",parentMenuId);
        if(StringUtils.isNotBlank(releaseId)){
            EnrollmentRelease enrollmentRelease=enrollmentReleaseService.selectByPrimaryKey(releaseId);
            view.addObject("enrollmentRelease",enrollmentRelease);
        }
        if (StringUtils.isNotBlank(id)) {
            view.addObject("enrollmentStuinfo",enrollmentStuinfoService.selectByPrimaryKey(id));
            EnrollmentHomeinfoVo enrollmentHomeinfoVo=new EnrollmentHomeinfoVo();
            EnrollmentTestVo enrollmentTestVo=new EnrollmentTestVo();
            enrollmentTestVo.setStuinfoId(enrollmentStuinfo.getId());
            enrollmentHomeinfoVo.setStuinfoId(enrollmentStuinfo.getId());
            List<EnrollmentHomeinfo> enrollmentHomeinfoList=enrollmentHomeinfoService.listExample(enrollmentHomeinfoVo);
            List<EnrollmentTest> enrollmentTestList=enrollmentTestService.listExample(enrollmentTestVo);
            view.addObject("homeinfoList",enrollmentHomeinfoList);
            view.addObject("testList",enrollmentTestList);
        }
        return view;
    }
    /**
     * 跳转统计分析界面
     *
     * @param releaseId 发布id
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @BaseLog(module = "自主招生", classz = "招生学生信息", methods = "跳转统计分析界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentstuinfo/getTj", method = RequestMethod.GET)
    public ModelAndView getTj(String releaseId,String parentMenuId,String applyId) {
        ModelAndView view = new ModelAndView("/work/enrollment/enrollmentstuinfo/enrollmentStuinfoTj");
        view.addObject("releaseId",releaseId);
        view.addObject("parentMenuId",parentMenuId);
        view.addObject("applyId",applyId);
        if(StringUtils.isNotBlank(releaseId)){
            view.addObject("enrollmentRelease",enrollmentReleaseService.selectByPrimaryKey(releaseId));
            EnrollmentStuinfoVo enrollmentStuinfoVo=new EnrollmentStuinfoVo();
            enrollmentStuinfoVo.setReleaseId(releaseId);
            List<EnrollmentStuinfo> schoolList=enrollmentStuinfoService.getSchoolNum(enrollmentStuinfoVo);
            //报名生源分析
            if(schoolList!=null&&schoolList.size()>0){
                String schoolName=schoolList.get(0).getSchool();
                String schoolNum=schoolList.get(0).getNum();
                if(schoolList.size()>=1){
                    for(int i=1;i<schoolList.size();i++){
                        schoolName=schoolName+","+schoolList.get(i).getSchool();
                        schoolNum=schoolNum+","+schoolList.get(i).getNum();
                    }
                    view.addObject("schoolNum",schoolNum);
                }else{
                    view.addObject("schoolNum",schoolNum);
                }
                view.addObject("schoolName",schoolName);
            }
            //生源地域分析
            List<EnrollmentStuinfo> schoolPlaceList=enrollmentStuinfoService.getSchoolPlaceNum(enrollmentStuinfoVo);
            if(schoolPlaceList!=null&&schoolPlaceList.size()>0){
                String schoolPlaceName=schoolPlaceList.get(0).getSchoolPlace();
                String schoolPlaceNum=schoolPlaceList.get(0).getNum();
                if(schoolPlaceList.size()>=1){
                    for(int i=1;i<schoolPlaceList.size();i++){
                        schoolPlaceName=schoolPlaceName+","+schoolPlaceList.get(i).getSchoolPlace();
                        schoolPlaceNum=schoolPlaceNum+","+schoolPlaceList.get(i).getNum();
                    }
                    view.addObject("schoolPlaceNum",schoolPlaceNum);
                }else{

                    view.addObject("schoolPlaceNum",schoolPlaceNum);
                }
                view.addObject("schoolPlaceName",schoolPlaceName);
            }
            List<EnrollmentStuinfo> schoolSexList=enrollmentStuinfoService.getSexNum(enrollmentStuinfoVo);
            if(schoolSexList!=null&&schoolSexList.size()>0){
                String boy=schoolSexList.get(0).getNum();
                String girl="";
                for(int i=1;i<schoolSexList.size();i++){
                    girl=schoolSexList.get(i).getNum();
                }
                view.addObject("boy",boy);
                view.addObject("girl",girl);
            }
        }
        return view;
    }

    /**
     * 保存或更新
     *
     * @param request             请求对象
     * @param enrollmentStuinfoVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @BaseLog(module = "自主招生", classz = "招生学生信息", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentstuinfo/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, EnrollmentStuinfoVo enrollmentStuinfoVo) {
        if(StringUtils.isNotBlank(enrollmentStuinfoVo.getReleaseId())){
            EnrollmentReleaseVo enrollmentReleaseVo=new EnrollmentReleaseVo();
            enrollmentReleaseVo.setId(enrollmentStuinfoVo.getReleaseId());
            List<EnrollmentRelease> enrollmentReleaseList=enrollmentReleaseService.listExample(enrollmentReleaseVo);
            if(enrollmentReleaseList!=null&&enrollmentReleaseList.size()>0){
                EnrollmentRelease enrollmentRelease=enrollmentReleaseList.get(0);
                if(enrollmentRelease.getState().equals("1")){
                    return new ReturnResult(ErrorCode.CODE_1,"该招生未发布，无法报名！");
                }else if(enrollmentRelease.getState().equals("3")){
                    return new ReturnResult(ErrorCode.CODE_1,"该招生已结束，无法报名！");
                }else if(enrollmentRelease.getState().equals("2")&&(new Date().before(enrollmentRelease.getStartTime()))){
                    return new ReturnResult(ErrorCode.CODE_1,"该招生未开始，无法报名！");
                }else if(enrollmentRelease.getState().equals("2")&&(new Date().after(enrollmentRelease.getEndTime()))){
                    enrollmentRelease.setState("3");
                    enrollmentRelease.setUpdateTime(new Date());
                    enrollmentReleaseService.updateByPrimaryKey(enrollmentRelease);
                    return new ReturnResult(ErrorCode.CODE_1,"该招生已结束，无法报名！");
                }else{
                    return enrollmentStuinfoService.addOrUpdate(enrollmentStuinfoVo, acceptCurrentUser(request));
                }
            }else{
                return new ReturnResult(ErrorCode.CODE_1,"不存在此次招生,请联系管理员！");
            }
        }else{
            return new ReturnResult(ErrorCode.CODE_1,"不存在此次招生,请联系管理员！");
        }
    }


    /**
     * 审核
     *
     * @param request             请求对象
     * @param enrollmentStuinfoVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @BaseLog(module = "自主招生", classz = "招生学生信息", methods = "审核", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentstuinfo/check", method = RequestMethod.POST)
    public ReturnResult check(HttpServletRequest request, EnrollmentStuinfoVo enrollmentStuinfoVo) {
        return enrollmentStuinfoService.check(enrollmentStuinfoVo, acceptCurrentUser(request));
    }

    /**
     * 一键审核
     *
     * @param request             请求对象
     * @param enrollmentStuinfoVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @BaseLog(module = "自主招生", classz = "招生学生信息", methods = "一键审核", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentstuinfo/checkAll", method = RequestMethod.POST)
    public ReturnResult checkAll(HttpServletRequest request, EnrollmentStuinfoVo enrollmentStuinfoVo) {
        return enrollmentStuinfoService.checkAll(enrollmentStuinfoVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request             请求对象
     * @param enrollmentStuinfoVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @BaseLog(module = "自主招生", classz = "招生学生信息", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentstuinfo/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, EnrollmentStuinfoVo enrollmentStuinfoVo) {
        return enrollmentStuinfoService.remove(enrollmentStuinfoVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件物理删除
     *
     * @param request             请求对象
     * @param enrollmentStuinfoVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @BaseLog(module = "自主招生", classz = "招生学生信息", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentstuinfo/delete", method = RequestMethod.GET)
    public ReturnResult delete(HttpServletRequest request, EnrollmentStuinfoVo enrollmentStuinfoVo) {
        return enrollmentStuinfoService.delete(enrollmentStuinfoVo, acceptCurrentUser(request));
    }

    /**
     * 数据导出
     *
     * @param enrollmentStuinfoVo 传入参数对象
     * @param response         响应对象
     * @return void
     * @author caideyong
     * @date 2019/11/27
     */
    @BaseLog(module = "自主招生", classz = "招生学生信息", methods = "数据导出", businessType = BusinessType.EXPORT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentstuinfo/export", method = RequestMethod.POST)
    public void export(HttpServletResponse response, EnrollmentStuinfoVo enrollmentStuinfoVo) {
        enrollmentStuinfoService.export(response, enrollmentStuinfoVo);
    }

    /**
     * 导入招生学生信息
     * @author caideyong
     * @date 2019/11/27
     * @return ReturnResult
     */
    @BaseLog(module = "自主招生", classz = "招生学生信息", methods = "导入招生学生信息", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentstuinfo/importData", method = RequestMethod.POST)
    public ReturnResult importData(HttpServletRequest request, MultipartFile file, String catalog, String update, String applyId,String releaseId) {
        return enrollmentStuinfoService.importData(request, file, catalog, update, applyId, acceptCurrentUser(request),releaseId);
    }
}